package com.ItemsRent.action;

import java.util.HashMap;

import org.apache.struts2.ServletActionContext;

import com.ItemsRent.IService.ICustomer;
import com.ItemsRent.model.Customer;
import com.ItemsRent.exception.MyHttpException;
/**
 * 
 * @author yrf
 *
 */
public class CustomerAction {
	
	private String cuName;
	private String qqNumber;
	private String phoneNumber;
	private String address;
	private String email;
	private String grade;
	private String old_pwd;
	private String new_pwd1;
	private String new_pwd2;
	
	private ICustomer cuService;

	private HashMap<String, Object> result = new HashMap<String, Object>();;
	
	
	public HashMap<String, Object> getResult() {
		return result;
	}

	public void setResult(HashMap<String, Object> result) {
		this.result = result;
	}

	public String getOld_pwd() {
		return old_pwd;
	}

	public void setOld_pwd(String old_pwd) {
		this.old_pwd = old_pwd;
	}

	public String getNew_pwd1() {
		return new_pwd1;
	}
 
	public void setNew_pwd1(String new_pwd1) {
		this.new_pwd1 = new_pwd1;
	}

	public String getNew_pwd2() {
		return new_pwd2;
	}

	public void setNew_pwd2(String new_pwd2) {
		this.new_pwd2 = new_pwd2;
	}

	public String getCuName() {
		return cuName;
	}

	public void setCuName(String cuName) {
		this.cuName = cuName;
	}

	public String getQqNumber() {
		return qqNumber;
	}

	public void setQqNumber(String qqNumber) {
		this.qqNumber = qqNumber;
	}

	public String getPhoneNumber() {
		return phoneNumber;
	}

	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getGrade() {
		return grade;
	}

	public void setGrade(String grade) {
		this.grade = grade;
	}

	public ICustomer getCuService() {
		return cuService;
	}

	public void setCuService(ICustomer cuService) {
		this.cuService = cuService;
	}
	
	/**
	 * 修改用户信息
	 * @return
	 * @throws Exception
	 */
	public String cuEdit() throws Exception{
		Customer customer = (Customer) ServletActionContext.getRequest().getSession().getAttribute("customer");
		if(customer == null){
			throw new Exception("权限已过期，请重新登录");
		}
		else{
			System.out.println(cuName);
			if(cuName!=null)customer.setCustomerName(cuName);
			if(qqNumber!=null)customer.setQq(qqNumber);
			if(phoneNumber!=null)customer.setPhone(phoneNumber);
			if(address!=null)customer.setAddress(address);
			if(email!=null)customer.setEmail(email);
			if(grade!=null)customer.setCustomer_grade_id(Integer.parseInt(grade));
			
			cuService.update(customer);
			result.put("code", "003");
			result.put("msg", "用户信息修改成功");
			return "success";
		}
	}
	
	public String getCu() throws Exception{
		Customer customer = (Customer) ServletActionContext.getRequest().getSession().getAttribute("customer");
		if(customer == null){
			throw new Exception("权限已过期，请重新登录");
		}
		
		else{
			result.put("cuNumber", customer.getNumber());
			result.put("cuQQ", customer.getQq());
			result.put("phoneNumber", customer.getPhone());
			result.put("address", customer.getAddress());
			result.put("email", customer.getEmail());
			result.put("status", customer.getCustomerStatus());
			result.put("grade", customer.getCustomer_grade_id());
		}
		return "success";
	}
	
	/**
	 * 修改密码
	 * @return
	 * @throws Exception
	 */
	public String pwdEdit() throws Exception{
		Customer customer = (Customer) ServletActionContext.getRequest().getSession().getAttribute("customer");
		if(customer == null){
			
			throw new Exception("权限已过期，请重新登录");
			
		}
		if(!customer.getCustomerPassword().equals(cuService.encryptByMD5(old_pwd))){
			throw new Exception("原密码不正确,请重新输入");
			
		}
		else if(!new_pwd1.equals(new_pwd2)){
			throw new Exception("两次输入新密码不一致,请重新输入");
			
		}
		else{
			//System.out.println("原密码为"+old_pwd+"   新密码1为："+new_pwd1+"   新密码2为："+new_pwd2);
			customer.setCustomerPassword(cuService.encryptByMD5(new_pwd2));
			cuService.update(customer);
			result.put("code", "003");
			result.put("msg", "密码修改成功");
		}
		return "success";
	}
	
}
